//
//  LZVideoDetailTextCell.m
//  LzzizDemo
//
//  Created by xbnzd on 16/11/3.
//  Copyright © 2016年 xbnzd. All rights reserved.
//

#import "LZVideoDetailTextCell.h"
#import "GiftListLabel.h"

@interface LZVideoDetailTextCell()

@property (weak, nonatomic) IBOutlet UILabel *cuisineCellLabel;
@property (weak, nonatomic) IBOutlet UILabel *cuisineLabel;
@property (weak, nonatomic) IBOutlet UILabel *ingredientsCellLabel;
@property (weak, nonatomic) IBOutlet UILabel *ingredientsLabel;
@property (weak, nonatomic) IBOutlet UILabel *introductionCellLabel;
@property (weak, nonatomic) IBOutlet UILabel *IntroductionLabel;
@property (weak, nonatomic) IBOutlet UIButton *showMoreBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showMoreBtnHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showMoreBtnBottomHeight;

@end

@implementation LZVideoDetailTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configureLabel];
    // Initialization code
    self.cuisineCellLabel.text = LZLocalizedString(@"restaurant_cuisine", nil);
    self.ingredientsCellLabel.text = LZLocalizedString(@"ingredients", nil);
    self.introductionCellLabel.text = LZLocalizedString(@"description", nil);
}

#pragma mark -
#pragma mark 
- (void)loadCuisine:(NSString *)cuisine Ingredients:(NSString *)ingredients Introduction:(NSString *)introduction
{
    self.cuisineLabel.text = cuisine;
    self.ingredientsLabel.text = ingredients;
    self.IntroductionLabel.text = introduction;
    
//    设置行间距
//    [self setLabel:self.cuisineLabel Text:cuisine];
//    [self setLabel:self.ingredientsLabel Text:ingredients];
//    [self setLabel:self.IntroductionLabel Text:introduction];
    
    [self checkWhetherShowMoreBtn];
}

- (UILabel *)cellTitleCuisineLabel {
    return self.cuisineCellLabel;
}

- (UILabel *)cellCuisineLabel {
    return self.cuisineLabel;
}

- (UILabel *)cellTitleIngredientsLabel {
    return self.ingredientsCellLabel;
}

- (UILabel *)cellIngredientsLabel {
    return self.ingredientsLabel;
}

- (UILabel *)cellTitleIntroductionLabel {
    return self.introductionCellLabel;
}

- (UILabel *)cellIntroductionLabel
{
    return self.IntroductionLabel;
}


/**
 是否展示更多按钮

 @param sender <#sender description#>
 */
- (IBAction)showMoreBtnAction:(id)sender {
    
    UIButton *btn = (UIButton *)sender;
    
    if (self.IntroductionLabel.numberOfLines == 3) {
        self.IntroductionLabel.numberOfLines = 0;
        [btn setImage:[UIImage imageNamed:@"restaurant_view_more_up"] forState:UIControlStateNormal];
    }else{
        self.IntroductionLabel.numberOfLines = 3;
        [btn setImage:[UIImage imageNamed:@"restaurant_view_more_down"] forState:UIControlStateNormal];
    }
    
    if ( self.textCellDelegate && [self.textCellDelegate respondsToSelector:@selector(clickMoreBtnWithIndexPath:withBtn:)]) {
        [self.textCellDelegate performSelector:@selector(clickMoreBtnWithIndexPath: withBtn:) withObject:self.selfIndexPath withObject:self.showMoreBtn];
    }
}


/**
 考虑到每个label都有可能多行展示,这里对label进行赋值操作

 @param label <#label description#>
 @param text <#text description#>
 */
- (void)setLabel:(UILabel *)label Text:(NSString *)text
{
    if (text.length == 0) {
        return;
    }
    extern CGFloat textLineSpace;
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    
    [style setLineSpacing:textLineSpace];
    [style setLineBreakMode:NSLineBreakByTruncatingTail];
    [style setAlignment:NSTextAlignmentLeft];
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attStr.length)];
    label.attributedText = attStr;
}

- (void)checkWhetherShowMoreBtn
{
    GET_SCREEN_WIDTH(sWidth);
    CGFloat titleWidth = self.introductionCellLabel.intrinsicContentSize.width + 30;
    CGFloat oneLineH = [@"aaa" sizeWithAttributes:@{NSFontAttributeName : self.IntroductionLabel.font}].height;
//    CGFloat labelHeigh = [self.IntroductionLabel intrinsicContentSize].height;
    CGFloat labelHeigh = [self.IntroductionLabel.text boundingRectWithSize:CGSizeMake(sWidth - titleWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.IntroductionLabel.font} context:nil].size.height;
    int num = labelHeigh/oneLineH;
    
    if (num <= 3) {
        self.showMoreBtnHeight.constant = 0;
        self.showMoreBtnBottomHeight.constant = 0;
    }else{
        self.showMoreBtnHeight.constant = 31;
        self.showMoreBtnBottomHeight.constant = 10;
    }
    
}


#pragma mark -
#pragma mark configure label
- (void)configureLabel
{
//    [self.cuisineLabel setVerticalAlignment:VerticalAlignmentTop];
//    [self.ingredientsLabel setVerticalAlignment:VerticalAlignmentTop];
//    [self.IntroductionLabel setVerticalAlignment:VerticalAlignmentTop];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
